package cn.bx.bid.ctrl;

import cn.bx.bid.entity.AjaxResult;
import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.User;
import cn.bx.bid.service.UserService;
import cn.bx.bid.util.LogUtil;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class UserController {
   @Resource
    UserService userService;

   //用户登录

    @RequestMapping("login1")
    @ResponseBody
    public AjaxResult login1(HttpServletRequest request, @RequestParam("username") String username, @RequestParam("password") String pass){
        User u= userService.login(username,pass);
        if(u!=null){
            return AjaxResult.success("true",u);
        }

        return AjaxResult.error("false");
    }


    //删除用户
    @RequestMapping("userdel")
    @ResponseBody
    public AjaxResult del1(HttpServletRequest request, @RequestParam("id") long id){
        int u= userService.del(id);
        if(u>0){
            return AjaxResult.success("删除成功");
        }
        return  AjaxResult.error("删除失败");
    }

    //查询单个用户信息
    @RequestMapping("touser-look-{id}")
    public ModelAndView userLook(@PathVariable("id") long id){
         User user=  userService.get(id);
        System.out.println("1>>>>>>>>>>>>>"+user.getEnterdate());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
       String date=  simpleDateFormat.format(user.getEnterdate());
        System.out.println("2>>>>>>>>>>>>>"+date);
        ModelAndView mv=new ModelAndView();
        mv.addObject("user",user);
        mv.setViewName("userupdate");
        return mv;
    }

    //更新用户信息
    @RequestMapping(value = "userUpdate",produces = "application/json;utf-8")
    @ResponseBody
    public AjaxResult userUpdate(@ModelAttribute("u")User user){
       int a= userService.update(user);

        if(a>0){
            return AjaxResult.success("true");
        }
        return  AjaxResult.error("false");
    }

    //批量删除
    @RequestMapping(value = "batchDeluser",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult batchDel(@RequestParam("ids[]")long[] ids){
      int b=  userService.batchDel(ids);
      return b>0?AjaxResult.success("删除成功"):AjaxResult.error("删除失败");
    }

     //用户注册
    @GetMapping(value = "userAdd",produces = "application/json;charset=utf-8")
    @ResponseBody
    public  AjaxResult userAdd(@ModelAttribute("u") User user){
    //   UserService userService=SpringUtil.getBean(UserService.class);
          int a= userService.add(user);
          if(a>0){
              System.out.println("添加成功");
              return AjaxResult.success("true");
          }
         return AjaxResult.error("false");
    }

    //查询修改用户信息
    @GetMapping(value = "userGet",produces = "application/json;charset=utf-8")
    @ResponseBody
    public  User userGet(@RequestParam("id")long id){
        User u=  userService.get(id);
        return u;

    }

   //跳转增加用户
//    @RequestMapping("toUseradd")
//    public String adduser()
//    {
//        return "useradd";
//    }

   // checkUserNameRepeat
    @RequestMapping(value = "checkUserNameRepeat",produces = "application/json;charset=utf-8")
    public AjaxResult checkUserNameRepeat(@RequestParam("userName")String userName, @RequestParam(value = "id",required = false)Long id){
       boolean a= userService.checkUserNameRepeat(id,userName);
       if(a){
           return  AjaxResult.error("登录名重复");
       }
       return AjaxResult.success("登录名不存在");
    }


    //用户查询列表
    @GetMapping(value = "userSearch",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<User> search(@RequestParam(value = "page",defaultValue = "1") int pageNo, @RequestParam(value = "limit",defaultValue = "10") int limit, @RequestParam(value = "name",required = false) String name, @RequestParam(value = "startDate",required = false) Date start, @RequestParam(value = "endDate",required = false) Date end){
         Page<User> p=userService.paging(pageNo, limit, name, start, end);
         LogUtil.info("start:>>>>>>>>>>"+start);
         LogUtil.info("end:>>>>>>>>>>"+end);
        return p;

        /*  1.      ModelAndView mv=new ModelAndView();
       // mv.setViewName("userlist");
        mv.addObject("p",p);
        mv.addObject("name",name);

        mv.addObject("startDate",StringKit.toString(start));
        mv.addObject("endDate",StringKit.toString(end));
        return mv;
        */

    }

    //异常处理
    @ExceptionHandler
    public  AjaxResult error(Exception e){
        return  AjaxResult.error("false",e);
    }
    //设置日期格式
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }
}

